﻿using DevTool.Command;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevTool.Contraner
{
    public partial class BigFileReadControl : UserControl
    {
        private Encoding enoder = Encoding.Default;
        private string fileroot = string.Empty;
        public BigFileReadControl()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            InitializeComponent();

            var Encoders = ConfigHelper.AppSetting("Encoders");

            if (Encoders.Split(',').Length > 0)
            {
                this.cb_type.Items.AddRange(Encoders.Split(','));
                this.cb_type.SelectedIndex = 0;
            }

        }

        private void bt_open_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter = "文本文件|*.txt|所有|*.*";
            var sd = this.openFileDialog1.ShowDialog();
            if (sd == DialogResult.OK)
            {
                fileroot = this.openFileDialog1.FileName;
                Task.Run(() =>
                {
                    ReadBigFile(fileroot, enoder);
                });

            }
        }
        /// <summary>
        /// 大文本文件读写
        /// </summary>
        /// <param name="fileroot">文件地址</param>
        /// <param name="encoding">编码格式</param>
        /// <returns></returns>
        void ReadBigFile(string fileroot, Encoding encoding)
        {


            if (File.Exists(fileroot))
            {
                try
                {
                    this.rt_text.Clear();
                    using (var stream = new FileStream(fileroot, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
                    {
                        using (var bs = new BufferedStream(stream))
                        {
                            long totalsize = bs.Length;
                            long length = 1024 * 1024;
                            if (bs.Length < 1024)
                            {
                                length = bs.Length;
                            }
                            byte[] b = new byte[length];

                            while (bs.Read(b, 0, b.Length) > 0)
                            {
                                totalsize -= length;
                                this.rt_text.AppendText(encoding.GetString(b));
                                if (totalsize < length && totalsize > 0)
                                {
                                    b = new byte[totalsize];
                                }
                            }
                            bs.Flush();
                        }


                    }
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }

            }
            else
            {
                MessageBox.Show("文件不存在！");
            }


        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            var files = string.Empty;
            var str = this.rt_text.Text;
            if (string.IsNullOrEmpty(this.fileroot))
            {
                this.saveFileDialog1.Filter = "文本文件|*.txt|所有|*.*";
                var of = this.saveFileDialog1.ShowDialog();
                if (of == DialogResult.OK)
                {
                    Task.Run(() =>
                    {
                        try
                        {
                            files = this.saveFileDialog1.FileName;
                            File.WriteAllText(files, str, enoder);
                            MessageBox.Show("保存成功！", "提示");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    });



                }
            }
            else
            {
                Task.Run(() =>
                {
                    try
                    {
                        File.WriteAllText(this.fileroot, str, enoder);
                        MessageBox.Show("保存成功！", "提示");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                });

            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void cb_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cb_type.Text.Equals("系统默认"))
            {
                enoder = Encoding.Default;
            }
            else
            {
                enoder = Encoding.GetEncoding(this.cb_type.Text.Trim());

            }
            if (!string.IsNullOrEmpty(this.fileroot))
            {
                Task.Run(() =>
                {
                    ReadBigFile(this.fileroot, enoder);
                });

            }

        }
    }
}
